//
//  BZBaseViewController.m
//  Pods
//
//  Created by 郭艺伟 on 2017/9/25.
//
//

#import "BZBaseViewController.h"
//友盟统计SDK
#import <UMengAnalytics/UMMobClick/MobClick.h>
#define kUMAppKey @"5937cdd34544cb519a001f58"

#import <MJRefresh/MJRefresh.h>

@interface BZBaseViewController ()

/**
 * 是否正在加载
 */
@property (nonatomic, assign, getter=isLoading) BOOL loading;

/**
 * 是否展示的是加载页面
 */
@property (nonatomic, assign) BOOL showLoading;

/**
 * 是否是首次加载页面，防止一进入页面就显示空状态
 */
@property (nonatomic, assign) BOOL firstLoadPage;

@end

@implementation BZBaseViewController


/**
 *  第一次使用这个类的时候会调用(1个类只会调用1次)
 *  每个类只调用一次initialize。这个调用的时间发生在你的类接收到消息之前，但是在它的父类接收到initialize之后。
 */
+ (void)initialize
{
    //启动SDK的方法一般写在此处来保证一定执行，理论上说只要在主线程中调用，都会执行
    UMConfigInstance.appKey = kUMAppKey;
    UMConfigInstance.channelId = @"App Store";//一般是这样写，用于友盟后台的渠道统计，当然苹果也不会有其他渠道，写死就好
    UMConfigInstance.ePolicy =SEND_INTERVAL; //上传模式，这种为最小间隔发送90S，也可按照要求选择其他上传模式。也可不设置，在友盟后台修改。
    [MobClick startWithConfigure:UMConfigInstance];//开启SDK
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#warning edgesForExtendedLayout
    /* edgesForExtendedLayout是一个类型为UIExtendedEdge的属性，指定边缘要延伸的方向。
     因为iOS7鼓励全屏布局，它的默认值很自然地是UIRectEdgeAll，四周边缘均延伸，就是说，
     如果即使视图中上有navigationBar，下有*tabBar，那么视图仍会延伸覆盖到四周的区域
     */
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    //空视图
    self.firstLoadPage = YES;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    header.lastUpdatedTimeLabel.hidden=YES;
    [header setTitle:@"Pull down to refresh" forState:MJRefreshStateIdle];
    [header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
    [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
    self.listView.mj_header = header;
    
    self.listView.emptyDataSetSource = self;
    self.listView.emptyDataSetDelegate = self;
    if (!self.disableAutoBeginRefresh) {
        [self.listView.mj_header beginRefreshing];
    }
    
    if ([self.listView isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self.listView;
        if (tableView.style == UITableViewStylePlain) {
            tableView.tableFooterView = [[UIView alloc] init];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:NSStringFromClass([self class])];
    //("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

#pragma mark -

- (void)listViewBeginRefresh
{
    [self.listView.mj_header beginRefreshing];
}

- (void)listViewEndRefresh
{
    [self.listView.mj_header endRefreshing];
}

- (void)refreshData
{
    
}

- (BOOL)disableAutoBeginRefresh
{
    return NO;
}

#pragma mark -

- (UIScrollView *)listView
{
    return nil;
}

- (void)setLoading:(BOOL)loading
{
    if (self.isLoading == loading) {
        return;
    }
    
    _loading = loading;
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:17];
    attributes[NSForegroundColorAttributeName] = [UIColor colorWithWhite:0.66 alpha:1];
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"No data"
                                                                                   attributes:attributes];
    return attrString;
}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *imageName = @"img_empty";
    return [UIImage imageNamed:imageName];
}


- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.isLoading) {
        self.loading = NO;
        self.showLoading = YES;
        
        UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [view startAnimating];
        return view;
    } else {
        self.showLoading = NO;
        return nil;
    }
}


- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:17];
    attributes[NSForegroundColorAttributeName] = [UIColor colorWithWhite:0.3 alpha:1];
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"Tap to refresh"
                                                                                   attributes:attributes];
    return attrString;
}

#pragma mark - DZNEmptyDataSetDelegate

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    BOOL show = !self.firstLoadPage;
    self.firstLoadPage = NO;
    return show;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    BOOL allowTouch = !self.showLoading;
    return allowTouch;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return NO;
}

- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
{
    return self.isLoading;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    [self emptyStartLoading];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self emptyStartLoading];
}

- (void)emptyStartLoading
{
    self.loading = YES;
    self.showLoading = YES;
    [self.listView reloadEmptyDataSet];
    
    [self refreshData];
}


@end
